package com.demo.mq.comsumer;

import com.demo.config.RabbitMqConfig;
import com.demo.entity.DogDto;
import org.springframework.amqp.AmqpRejectAndDontRequeueException;
import org.springframework.amqp.ImmediateAcknowledgeAmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.*;
import java.time.LocalDateTime;

@Component
public class MqConsumer {
    @RabbitListener(queues = RabbitMqConfig.queue1)
    public void c1(Message message) {

        System.out.print("消费者消费 q1 信息，时间：" + LocalDateTime.now());
        System.out.println(new String(message.getBody()));

        //模拟异常
        throw new RuntimeException();
    }

    @RabbitListener(queues = RabbitMqConfig.queue2)
    public void c2(DogDto o, Message message) throws IOException, ClassNotFoundException {
        System.out.println("消费者消费 q2 信息：");

        //DogDto o = (DogDto)new ObjectInputStream(new ByteArrayInputStream(message.getBody())).readObject();
//        Map<String, Object> map = (Map)new ObjectInputStream(new ByteArrayInputStream(message.getBody())).readObject();
        //System.out.println( map.get("name") );
        //System.out.println( map.get("dog") );
        //DogDto o = new ObjectMapper().readValue(message.getBody(), DogDto.class);
        System.out.println(o);
    }
}
